package com.aaa.forum.controller;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.config.AppConfig;
import com.aaa.forum.model.Article;
import com.aaa.forum.model.Bookmark;
import com.aaa.forum.model.User;
import com.aaa.forum.services.IArticleService;
import com.aaa.forum.services.IBookmarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.util.List;

@Api(tags = "收藏接口")
@Slf4j
@RestController
@RequestMapping("/bookmark")
public class BookmarkController {

    @Resource
    private IBookmarkService bookmarkService;

    @Resource
    private IArticleService articleService;

    @ApiOperation("收藏帖子")
    @PostMapping("/mark")
    public AppResult mark(HttpServletRequest request,
                          @ApiParam("帖子id") @RequestParam("articleId") Long articleId) {
        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用service
        bookmarkService.mark(user.getId(), articleId);
        return AppResult.success();
    }

    @ApiOperation("取消收藏")
    @PostMapping("/cancel")
    public AppResult cancel(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") Long articleId) {
        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 调用service
        bookmarkService.cancel(user.getId(), articleId);
        return AppResult.success();
    }

    @ApiOperation("取消收藏")
    @PostMapping("/cancelByList")
    public AppResult cancelByList(HttpServletRequest request,
                                  @ApiParam("markId数组") @RequestBody List<Long> cancelArray) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        for (Long id : cancelArray) {
            // 获取收藏记录
            Bookmark bookmark = bookmarkService.selectById(id);
            // 校验记录是否存在
            if (bookmark == null || bookmark.getDeleteState() == 1) {
                log.warn(ResultCode.FAILED_BOOKMARK_NOT_EXISTS.toString());
                return AppResult.failed(ResultCode.FAILED_BOOKMARK_NOT_EXISTS);
            }
            // 校验是否属于该用户
            if (bookmark.getUserId() != user.getId()) {
                log.warn(ResultCode.FAILED_FORBIDDEN.toString());
                return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
            }
            // 调用service
            bookmarkService.cancelById(id);
        }
        return AppResult.success();
    }

    static class MoveData {
        public List<Long> selectArray;
        public Long collectionId;
    }

    @ApiOperation("移动收藏夹")
    @PostMapping("/moveCollection")
    public AppResult moveCollection(HttpServletRequest request,
                                    @ApiParam("自定义类") @RequestBody MoveData moveData) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (moveData.collectionId == null) {
            moveData.collectionId = 0L;
        }
        for (Long id : moveData.selectArray) {
            // 获取收藏记录
            Bookmark bookmark = bookmarkService.selectById(id);
            // 校验记录是否存在
            if (bookmark == null || bookmark.getDeleteState() == 1) {
                log.warn(ResultCode.FAILED_HISTORY_NOT_EXISTS.toString());
                return AppResult.failed(ResultCode.FAILED_HISTORY_NOT_EXISTS);
            }
            // 校验是否属于该用户
            if (bookmark.getUserId() != user.getId()) {
                log.warn(ResultCode.FAILED_FORBIDDEN.toString());
                return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
            }
            // 调用service
            bookmarkService.modifyCollectionId(id, moveData.collectionId);
        }
        return AppResult.success();
    }
}
